package cc.mrbird.febs.business.service.impl;

import cc.mrbird.febs.business.dao.CommunityMapper;
import cc.mrbird.febs.business.domain.Community;
import cc.mrbird.febs.business.service.CommunityService;
import cc.mrbird.febs.common.domain.QueryRequest;
import cc.mrbird.febs.common.utils.SortUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("communityService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements CommunityService {

    @Override
    public IPage<Community> findCommunitys(Community community, QueryRequest request) {
        try {
            LambdaQueryWrapper<Community> queryWrapper = new LambdaQueryWrapper<>();

            if (StringUtils.isNotBlank(community.getCommName())) {
                queryWrapper.eq(Community::getCommName, community.getCommName());
            }
            Page<Community> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page,queryWrapper);
        } catch (Exception e) {
            log.error("获取店铺信息失败", e);
            return null;
        }
    }

    @Override
    public Community findByName(String commName) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Community>().eq(Community::getCommName, commName));
    }

    @Override
    public void createCommunity(Community community) {
        community.setCreateTime(new Date());
        this.save(community);
    }

    @Override
    public void deleteCommunitys(String[] communityIds) throws Exception {
        List<String> list = Arrays.asList(communityIds);
        baseMapper.deleteBatchIds(list);
    }

    @Override
    public void updateCommunity(Community community) throws Exception {
        community.setModifyTime(new Date());
        baseMapper.updateById(community);
    }

}
